<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_produk_diskon extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_produk_diskon_model', 'PD');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Produk Diskon";
		viewByTemplate('kst_produk_diskon/index', $data, 'grid');
	}
	 
	function getdata()
	{
		$this->secure_session();
		echo $this->PD->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->PD->GetJSON2();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("barang_nama") == '') {
			$error->Add("barang_nama", "Nama Barang", $tpl_empty);
		}else if (post("barang_harga") == '') {
			$error->Add("barang_harga", "Harga", $tpl_empty);
		}
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$query = $this->db->query("SELECT pd_id FROM (SELECT * FROM kst_produk_diskon ORDER BY pd_id DESC) WHERE rownum <= 1");
				if ($query->row_array()) {
					$getquery = $query->row_array();
					$rowcount = $getquery['pd_id'];
					$autonum = $rowcount + 1;
				} else {
					$autonum = 1;
				}


				$tgl_old = str_replace(" ", "", $this->input->post('pd_awal'));
				$tahun_new = substr($tgl_old, 6, 4);
			    $bulan_new = substr($tgl_old, 3, 2);
			    $hari_new   = substr($tgl_old, 0, 2);
			    $tgl_new = $tahun_new."-".$bulan_new."-".$hari_new;

				$tgl_old1 = str_replace(" ", "", $this->input->post('pd_akhir'));
				$tahun_new1 = substr($tgl_old1, 6, 4);
			    $bulan_new1 = substr($tgl_old1, 3, 2);
			    $hari_new1   = substr($tgl_old1, 0, 2);
			    $tgl_new1 = $tahun_new1."-".$bulan_new1."-".$hari_new1;


	 			$data = array(
					'pd_id' => $autonum,
					'barang_nama' => $this->input->post('barang_nama'),
					'barang_harga' => $this->input->post('barang_harga'),
					'pd_persen' => $this->input->post('pd_persen'),
					'pd_diskon' => $this->input->post('pd_diskon'),
					'pd_harga' => $this->input->post('pd_harga'),
					'pd_awal' => $tgl_new,
					'pd_awal_jam' => $this->input->post('pd_awal_jam'),
					'pd_awal_menit' => $this->input->post('pd_awal_menit'),
					'pd_akhir' => $tgl_new1,
					'pd_akhir_jam' => $this->input->post('pd_akhir_jam'),
					'pd_akhir_menit' => $this->input->post('pd_akhir_menit'),
					'pd_status' => $this->input->post('pd_status'),
					);
				
				if (post("mode") == "create")
				{
					$this->PD->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->PD->Update($data, array('pd_id'	=> $data['pd_id']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$pd_id	= get('pd_id');
		
		if ($pd_id != "")
		{
			// Delete Single
			$arr_rule	= array('pd_id' => $pd_id);
			$ls_result = $this->PD->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('pd_id' => $row['pd_id']);
				$ls_result .= $this->PD->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>